﻿#include "login.h"
#include "ui_login.h"
#include "detection.h"
#include "myqlabel.h"
#include "forgetpassword.h"
#include"stackwidget.h"

#include "connectionpool.h"
#include "titlebar.h"
#include "titlebarfix.h"

#include <QSqlTableModel>
#include <QMessageBox>
#include<QSettings>
#include<qDebug>
#include <QStandardPaths>
#include"connectionpool.h"
//字符编码设为utf-8
//#pragma execution_character_set("utf-8")
QString login::Qname="";
login::login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::login)
{

    ui->setupUi(this);

//    this->setAttribute(Qt::WA_DeleteOnClose, true);
    //设置无边框
    this->setWindowFlags(Qt::FramelessWindowHint);

    //背景图片等比缩放
//    QPalette PAllbackground = this->palette();
//    QImage ImgAllbackground(":/images/back.png");
//    QImage fitimgpic=ImgAllbackground.scaled(this->width(),this->height(), Qt::IgnoreAspectRatio);
//    PAllbackground.setBrush(QPalette::Window, QBrush(fitimgpic));
//    this->setPalette(PAllbackground);
    //设置窗口图标aa70
//    this->setWindowIcon(QIcon(":/images/软件图标.png"));
    //界面窗口固定
    this->setFixedSize(819,458);

    //读取user.ini配置文件
    this->readcfg();

}
login::~login()
{
    delete ui;
}
//保存配置信息，记住密码
void login::savecfg()
{
//    QString config_path=QStandardPaths::standardLocations(QStandardPaths::HomeLocation)+"/.Colorectal_user.ini";
//    QSettings settings(config_path,QSettings::IniFormat);
    QSettings settings(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.Colorectal_user.ini",QSettings::IniFormat);
//    QSettings settings("user.ini",QSettings::IniFormat);
    bool IsRemember=ui->checkBox_rememberP->isChecked();
    qDebug()<<"login is remmber:"<<IsRemember;
    if(IsRemember){
        settings.setValue("username",ui->lineEdit_username->text());
        settings.setValue("password",ui->lineEdit_password->text());
        settings.setValue("IsRemember",ui->checkBox_rememberP ->isChecked());

    }
    else{
        settings.setValue("IsRemember",false);
    }
    settings.setValue("IsAutoLogin",ui->checkBox_autoLogin->isChecked());
}
//读取配置信息
void login::readcfg()
{
//    QString config_path="C:/Colorectal_user.ini";
//    QSettings settings(config_path,QSettings::IniFormat);
//    QSettings settings("user.ini",QSettings::IniFormat);
    QSettings settings(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.Colorectal_user.ini",QSettings::IniFormat);
    bool IsRemember=settings.value("IsRemember").toBool();
    qDebug()<<"login is remmber:"<<IsRemember;
    if(IsRemember){
        QString username = settings.value("username").toString();
        QString password = settings.value("password").toString();
        ui->lineEdit_username->setText(username);
        ui->lineEdit_password->setText(password);
        ui->checkBox_rememberP->setChecked(IsRemember);
    }
    bool IsAutoLogin=settings.value("IsAutoLogin").toBool();
    ui->checkBox_autoLogin->setChecked(IsAutoLogin);

    //全局flag,用于判断是否自动登录
    this->autoLoginFlag=IsAutoLogin;
    this->rememberPasswordFlag=IsRemember;

}

//外界调用的
void login::autoLoginByButton()
{
    this->on_pushButton_ok_clicked();
}

void login::on_pushButton_ok_clicked()
{

    Qname = ui->lineEdit_username->text();
    QString passwd = ui->lineEdit_password->text();

    if(passwd!=nullptr)
    {
        //db
//        QSqlDatabase db =Conne::openConnection();

        QString sql_user=QString("select *from user where user_name='%1' and password= '%2'").arg(Qname).arg(passwd);
//        QSqlTableModel model;
//        model.setTable("user");
//        model.setFilter(tr("user_name = '%1' and password = '%2'").arg(Qname).arg(passwd));

        qDebug()<<QStringLiteral("登录sql:")<<sql_user;
        QSqlQuery query;
        query.exec(sql_user);
        qDebug()<<QStringLiteral("登录用户")<<query.next();
        qDebug()<<QStringLiteral("登录用户")<<query.value(0).toString();
//        model.select();
        if(query.size() == 1)
        {
            //是否记住密码，配置文件
            this->savecfg();

            //发送关闭状态栏信号
            emit sendsignal();
            this->close();
            StackWidget *stackw=new StackWidget;
//            stackw->setAttribute(Qt::WA_DeleteOnClose, true);
//            //自定义标题栏
            TitleBar *tBar=new TitleBar;
//            stackw->setAttribute(Qt::WA_DeleteOnClose, true);

            connect(stackw,SIGNAL(sendsignal()),tBar,SLOT(close()));
            tBar->setMainWidget(stackw);
            tBar->setTitleBarText(QStringLiteral("检测主界面"));
            tBar->show();
//            stackw->show();

        }
        else
        {
            QMessageBox::warning(NULL, QStringLiteral("登陆失败"), QStringLiteral("用户名或密码错误，请重新输入！"));
            ui->lineEdit_username->clear();
            ui->lineEdit_password->clear();
        }
    }
    else {
        QMessageBox::warning(NULL, QStringLiteral("登陆失败"), QStringLiteral("密码错误，请重新输入！"));
//        ui->lineEdit_username->clear();
        ui->lineEdit_password->clear();
    }

}

void login::on_pushButton_cacel_clicked()
{
    //发送关闭自定义状态栏信号
    emit sendsignal();
    this->close();
}

void login::on_label_foget_password_clicked()
{
    forgetPassword*forgetP=new forgetPassword;

    //自定义标题栏
    titlebarFix *tBar2=new titlebarFix;

    connect(forgetP,SIGNAL(sendsignal()),tBar2,SLOT(close()));
    tBar2->setAttribute(Qt::WA_DeleteOnClose, true);
    tBar2->setMainWidget(forgetP);
    tBar2->setTitleBarText(QStringLiteral("身份验证"));
    tBar2->show();



}
